var path = require('path');
var fs = require('fs');
var config = require("./config");
var ueditor = require("ueditor");
var Index = require("./app/controllers/index");
var Admin = require("./app/controllers/admin");
var filepath = './api/articles.json';

module.exports = function (app) {
    // ueditor 入口地址配置 https://github.com/netpi/ueditor/blob/master/example/public/ueditor/ueditor.config.js 
    app.use("/ueditor/ue", ueditor(path.join(__dirname, 'public/upload'), function(req, res, next) {
        var imgDir = '/image' //图片默认上传地址
        var ActionType = req.query.action;
        if (ActionType === 'uploadimage' || ActionType === 'uploadfile' || ActionType === 'uploadvideo') {
            var file_url = imgDir;      //图片默认上传地址
            /*其他上传格式的地址*/
            if (ActionType === 'uploadfile') {
                file_url = '/file';     //附件保存地址 
            }
            if (ActionType === 'uploadvideo') {
                file_url = '/video';    //视频保存地址 
            }
            res.ue_up(file_url);        //你只要输入要保存的地址。保存操作交给ueditor
            res.setHeader('Content-Type', 'text/html');
        } else if (ActionType === 'listimage') {
            //客户端发起图片列表请求
            res.ue_list(imgDir);        // 客户端会列出 dir_url 目录下的所有图片 
        } else {
            // 客户端发起其它请求
            res.setHeader('Content-Type', 'application/json');
            res.redirect('/ueditor/nodejs/config.json');
        }
    }));

    app.get(['/', '/index', '/home'], Index.index);

    app.get('/post', Index.post);

    app.get('/editor', Index.editor);

    app.get('/article/:id', Index.article);

    app.post('/postArticle', function (req, res) {
        var params = req.body;
        if (params.innerHTML != '') {
            var result = JSON.parse(fs.readFileSync(filepath));
            if (result[params.articleid]) {
                if(params.update) {
                    result[params.articleid] = {
                        'innerHTML': params.innerHTML
                    };
                    fs.writeFileSync(filepath, JSON.stringify(result));
                    res.send({
                        status: 1,
                        msg: '文章已更新!'
                    });
                } else {
                    res.send({
                        status: 0,
                        msg: '发现重复文章,请勾选更新按钮重新提交'
                    });
                }
            } else {
                result[params.articleid] = {
                    'articleid': params.articleid,
                        'title': params.title,
                        'cates': params.cates,
                        'date' : params.date,
                        'author': params.author,
                        'imgid': params.imgid,
                    'innerHTML': params.innerHTML
                };
                fs.writeFileSync(filepath, JSON.stringify(result));
                res.send({
                    status: 1,
                    msg: 'ok'
                });
            }
        } else {
            res.send({
                status: 0,
                msg: "文章不能为空"
            });
        }
    });

    // 后台管理:
    app.get('/admin/login', function (req, res) {
        res.render('admin/login', { title: '登录' });
    });

    app.post('/login', Admin.postLogin);

    app.get('/logout', Admin.logout);                   //从地址栏退出登录
    app.post('/logout', Admin.postLogout);              //使用退出按钮退出登录

    app.get('/admin', Admin.isLogin, Admin.admin);      //后台管理首页

    app.get('/admin/articles', Admin.isLogin, Admin.articles);      //文章列表

};